package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import anim.Animation;

import vis.GPoint;
import vis.GPolygon;
import vis.Scene;
import vis.TextLabel;
import vis.Viewport;

public class MainWindow extends JFrame implements ActionListener, MouseListener {

	private Viewport vport = null;
	private Scene scene = null;
	private GPolygon[] poly = new GPolygon[2];
	private int mode = 0;
	private int cp = 0;
	private Animation anim = null;
	private TextLabel label = null;
	
	private JButton btnPrvi, btnDrugi, btnClearPrvi, btnClearDrugi, btnIntersect;
	
	public MainWindow()
	{
		super("Presjek monotonih poligona (09/08)");
		
		scene = new Scene();
		vport = new Viewport(scene);
		poly[0] = new GPolygon();
		poly[1] = new GPolygon();
		scene.addObject(poly[0]);
		scene.addObject(poly[1]);
		
		vport.setOffset(400, 300);
		vport.addVPListener(this);
		anim = new Animation(vport);
		label = new TextLabel("");
		scene.addObject(label);
		
		this.setLayout(new BorderLayout());
		
		add(vport, BorderLayout.CENTER);
		
		JPanel cpanel = new JPanel();
		btnPrvi = new JButton("Dodaj tacku u prvi poligon");
		btnPrvi.addActionListener(this);
		cpanel.add(btnPrvi);
		
		btnDrugi = new JButton("Dodaj tacku u drugi poligon");
		btnDrugi.addActionListener(this);
		cpanel.add(btnDrugi);
		
		btnClearPrvi = new JButton("Resetuj prvi poligon");
		btnClearPrvi.addActionListener(this);
		cpanel.add(btnClearPrvi);
		
		btnClearDrugi = new JButton("Resetuj drugi poligon");
		btnClearDrugi.addActionListener(this);
		cpanel.add(btnClearDrugi);
		
		btnIntersect = new JButton("Pronadji presjek");
		btnIntersect.addActionListener(this);
		cpanel.add(btnIntersect);
		
		add(cpanel, BorderLayout.SOUTH);
		
		setSize(800, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new MainWindow();
	}
	
	private void resetView()
	{
		scene.reset();
		label.setText("");
		scene.addObject(poly[0]);
		scene.addObject(poly[1]);
		scene.addObject(label);
		repaint();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == btnPrvi)
		{
			mode = 0;
			cp = 0;
		}
		else if(arg0.getSource() == btnDrugi)
		{
			mode = 0;
			cp = 1;
		}
		else if(arg0.getSource() == btnClearPrvi)
		{
			poly[0].clear();
			//vport.repaint();
			resetView();
		}
		else if(arg0.getSource() == btnClearDrugi)
		{
			poly[1].clear();
			//vport.repaint();
			resetView();
		}
		else if(arg0.getSource() == btnIntersect)
		{
			if(poly[0].getVertexCount() < 3 || poly[1].getVertexCount() < 3)
			{
				System.out.println("Moraju imati bar po 3 tacke");
				return;
			}
			resetView();
			
			anim.seekTo(0);
			anim.clear();
			boolean c1 = poly[0].checkXMonotone(anim, label);
			if(!c1)
			{
				System.out.println("Nije monoton!");
			}
			else
			{
				boolean c2 = poly[1].checkXMonotone(anim, label);
				if(!c2)
				{
					System.out.println("Nije monoton!");
				}
				else
				{
					GPolygon.intersectMonotone(poly[0], poly[1], label, anim, scene, vport);
				}
			}
			anim.startAnim();
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {}

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		if(mode == 0 && !anim.isRunning())
		{
			poly[cp].addPoint(new GPoint(arg0.getX(), arg0.getY()));
			vport.repaint();
		}
		
	}

}
